package com.huabo.cybermonitor.controller;

import cn.hutool.core.collection.SpliteratorUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbfk.entity.DealUserToken;
import com.hbfk.entity.TblOrganizationUtil;
import com.hbfk.entity.TblStaffUtil;
import com.hbfk.util.JedisUtil;
import com.hbfk.util.JsonBean;
import com.hbfk.util.RedisFinalUtis;
import com.huabo.cybermonitor.entity.*;
import com.huabo.cybermonitor.mapper.YhrPageMapper;
import com.huabo.cybermonitor.service.*;
import com.huabo.cybermonitor.service.base.CJobTaskService;
import com.huabo.cybermonitor.task.base.JobTaskService;
import com.huabo.cybermonitor.task.base.ScheduleJob;
import com.huabo.cybermonitor.util.ConstClass;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.mockito.internal.matchers.Or;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;

@RestController
@Slf4j
@Api(value = "指标监控-指标管理", tags = {"指标监控-指标管理监控所有接口"})
@RequestMapping(value = "/cyber/ZbglController")
@SuppressWarnings("all")
public class ZbglController {
    private static final Logger logger = LoggerFactory.getLogger(MxglController.class);

    @Autowired
    IOrganizationService iOrganizationService;

    @Autowired
    TreeService treeService;

    @Autowired
    IIndicatorService iIndicatorService;

    @Autowired
    IStaffService iStaffService;

    @Autowired
    IBiDatasourceService iBiDatasourceService;


    @Autowired
    IIndicatorthresholdService indicatorthresholdService;

    @Autowired
    IWarningResultService iWarningResultService;

    @Autowired
    IMonitorIndicatorresultService iMonitorIndicatorresultService;

    @Autowired
    IFlowService iFlowService;

    @Autowired
    IControlmatrixService iControlmatrixService;

    @Autowired
    IRiskeventService iRiskeventService;

    @Autowired
    IIndicatorCmService iIndicatorCmService;

    @Autowired
    IMonitorSolutionService iMonitorSolutionService;

    @Autowired
    ITblBiDatasourceService  iTblBiDatasourceService;



    @Resource
    YhrPageMapper pageMapper;
    /**
     * 行业指标库---main页面
     *
     * @return
     */
    @GetMapping(value = "/zbjk/info_index")
    @ApiOperation("行业指标库---main页面")
    public JsonBean info_index(@ApiParam(name = "ty", value = "ty") @RequestHeader("ty") String ty,
                               @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token) {
        if(!ConstClass.checkToken(token)){
            return ConstClass.tokenFailure();
        }
        logger.info("行业指标库---main页面");
        TblStaffUtil staff = null;
        TblOrganizationUtil organization = null;
        try {
            staff = DealUserToken.parseUserToken(token);
            organization = staff.getCurrentOrg(); //当前用户选择的组织hbOrgEntityOrgid
        } catch (Exception e) {
        }

        Map map = new HashMap();
        List<Organization> findOrgTree = iOrganizationService.findOrgTreeObjByHY(organization.getOrgid().toString());
        map.put("ty", ty);
        map.put("list", findOrgTree);
        map.put("number", findOrgTree.size());
        return new JsonBean(200, "成功", map);
    }


    /**
     * 指标管理-左侧菜单
     *
     * @param
     * @return
     */
    @GetMapping(value = "/zbjk/info_left")
    @ApiOperation("指标管理-左侧菜单")
    public JsonBean info_left(@ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token) {
        if(!ConstClass.checkToken(token)){
            return ConstClass.tokenFailure();
        }
        TblStaffUtil staff = null;
        try {
            staff = DealUserToken.parseUserToken(token);
        } catch (Exception e) {

        }
        Organization userOrg = iOrganizationService.belongToCompany(staff.getOrgid().toString());
        Map<String, Object> map = new HashMap<>(2);
        map.put("userOrgId", userOrg.getOrgid());// 查询 TblOrganization 表
        map.put("orgid", staff.getOrgid());
        return new JsonBean(200, "成功", map);
    }


    /**
     * 指标管理-列表
     */
    @GetMapping(value = "/zbjk/kri_info")
    @ApiOperation("指标管理-列表")
    public JsonBean kri_infoZbjk(@ApiParam(name = "orgId", value = "orgId") @RequestParam(value = "orgId",required = false) String orgId,
                                 @ApiParam(name = "kricode", value = "kricode") @RequestParam(value = "kricode",required = false) String kricode,
                                 @ApiParam(name = "kriname", value = "kriname") @RequestParam(value = "kriname",required = false) String kriname,
                                 @ApiParam(name = "pageNumber", value = "pageNumber") @RequestParam(value = "pageNumber",required = false, defaultValue = "1") Integer pageNumber,
                                 @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token
    ) {
        if(!ConstClass.checkToken(token)){
            return ConstClass.tokenFailure();
        }
        TblStaffUtil staff = null;
        TblOrganizationUtil attribute = null;
        TblOrganizationUtil attribute1 = null;
        try {
            staff = DealUserToken.parseUserToken(token);
            //选则的机构
            attribute = staff.getCurrentOrg();
            //当前用户的机构
            attribute1 = staff.getLinkOrg();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (attribute.getOrgid().toString().equals(attribute1.getOrgid().toString())) {
            if (StringUtils.isBlank(orgId)) {
                orgId = staff.getLinkDetp().getOrgid().toString();
            }
        }
        else {
            if (StringUtils.isBlank(orgId)) {
                orgId = attribute.toString();
            }
        }

        IPage ip = new Page(pageNumber, ConstClass.DEFAULT_SIZE);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("ORGID", orgId);
        if(StringUtils.isNotBlank(kriname)){
            queryWrapper.like("INDICATORNAME", kriname );
        }
        if(StringUtils.isNotBlank(kricode)){
            queryWrapper.like("INDICATORCODE", kricode);
        }


        queryWrapper.eq("indicatordb", Indicator.IS_HY0);
        queryWrapper.orderByDesc("createDate", "INDICATORID");
        iIndicatorService.page(ip, queryWrapper);
        Map map = new HashMap();
        map.put("orgid", orgId);
        map.put("tblStaff", staff);
        map.put("kricode", kricode);
        map.put("kriname", kriname);
        map.put("pageBean", ip);
        return new JsonBean(200, "成功", map);
    }

    /**
     * RedisOrg
     *
     * @param
     * @param
     * @returnhttp://huabodashuju.com/redisorg/findOrganizationByTreeAllbm?nodeId=116821
     */
    @GetMapping(value = "/zbjk/findOrganizationByTreeAllbm")
    @ApiOperation("通过redis获取左侧列表数据")
    public JsonBean findOrganizationByTreeAllbm(@ApiParam(name = "nodeId", value = "nodeId") @RequestParam(value = "nodeId", required = false) String nodeId,
                                                @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token) {
        if(!ConstClass.checkToken(token)){
            return ConstClass.tokenFailure();
        }
        return new JsonBean(200, "成功", "");
    }

//    lovexyyy@163.com
//    love0913

    /**
     * 指标管理-添加
     *
     * @param
     * @return
     */
    @PostMapping(value = "/zbjk/kri_info_add")
    @ApiOperation("指标管理-添加")
    public JsonBean kri_info_addZbjk(@ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token) {
        if(!ConstClass.checkToken(token)){
            return ConstClass.tokenFailure();
        }
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        TblStaffUtil staff = null;

        try {
            staff = DealUserToken.parseUserToken(token);

        } catch (Exception e) {
            e.printStackTrace();
        }

        //QueryWrapper<Organization> wrapper = new QueryWrapper<>();
        //wrapper.eq("orgid", staff.getLinkOrg().getOrgid());
        Organization organization = iOrganizationService.getById(staff.getLinkDetp().getOrgid());
        String orgid = organization.getOrgid().toString();
        //Organization og = iOrganizationService.getById(orgid);
        Map map = new HashMap();
        map.put("creater", staff.getUsername());
        map.put("creatTime", sf.format(new Date()));
        map.put("orgname", organization.getOrgname());
        map.put("orgid", orgid);
        map.put("staffid", staff.getStaffid());
        map.put("organization", organization);
        return new JsonBean(200, "成功", map);
    }

    /**
     * 指标管理-保存
     *
     * @return
     */
    @GetMapping(value = "/zbjk/kri_info_mod")
    @ApiOperation("指标管理-保存")
    public JsonBean kri_info_modZbjk(@ApiParam(name = "createdatea", value = "createdatea") @RequestParam(value = "createdatea") String createdatea,
                                     @ApiParam(name = "indicatordes", value = "indicatordes") @RequestParam(value = "indicatordes") String indicatordes,
                                     @ApiParam(name = "indicator", value = "indicator") @RequestBody() Indicator indicator,
                                     @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token) {
        if(!ConstClass.checkToken(token)){
            return ConstClass.tokenFailure();
        }
        indicatordes = indicatordes.trim();
        if (createdatea != null && !"".equals(createdatea)) {
            SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
            indicator.setCreatedate(LocalDateTime.parse(createdatea));
        } else {
            indicator.setCreatedate(LocalDateTime.now());
        }
        if (indicator.getIndicatorid() == null) {
            indicator.setRunstatus(new BigDecimal(0));
        }
        indicator.setIndicatordb(Indicator.IS_HY0);
        indicator.setIndicatordes(indicatordes != null ? indicatordes : null);
        iIndicatorService.save(indicator);

        Map map = new HashMap();
        map.put("selectProjectid", indicator.getIndicatorid());

        return new JsonBean(200, "成功", map);
    }


    /**
     * 指标管理-修改
     *
     * @param
     * @return
     */
    @SuppressWarnings("unchecked")
    @GetMapping(value = "/zbjk/kri_info_modife")
    @ApiOperation("指标管理-修改")
    public JsonBean kri_info_modZbjk_update(@ApiParam(name = "selectProjectid", value = "指标主键") @RequestParam(value = "selectProjectid") String selectProjectid,
                                            @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token) {
        if(!ConstClass.checkToken(token)){
            return ConstClass.tokenFailure();
        }
        Indicator indicator = null;
        List<Flow> fw = null;
        String flows = "";
        if (selectProjectid != null && !"".equals(selectProjectid)) {
            indicator = iIndicatorService.getById(selectProjectid);
            if(Objects.isNull(indicator)){
                return new JsonBean(500, selectProjectid + "没有找到指标",null);
            }

            fw = iFlowService.showListWithIndicatorid(indicator.getIndicatorid());
            for (Flow tblFlow : fw) {
                flows += tblFlow.getFlowid() + ",";
            }
        }
        Staff user = iStaffService.getById(indicator.getStaffid().toString());
        Map map = new HashMap();

        map.put("indicator", indicator);
        map.put("org", iOrganizationService.getById(indicator.getOrgid()));
        //全部flow
        map.put("flowList",fw);
        //所有编号
        map.put("flows", flows);
        map.put("creater", user);
        return new JsonBean(200, "成功", map);
    }


    /**
     * 指标管理-计算公式
     *
     * @param
     * @return
     */
    @GetMapping(value = "/zbjk/kri_info_formula")
    @ApiOperation("指标管理-计算公式")
    public JsonBean kri_info_formulaZbjk(@ApiParam(name = "selectProjectid", value = "selectProjectid") @RequestParam(value = "selectProjectid") String selectProjectid,
                                         @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token) {
        Map map = new HashMap();
        Indicator indicator = null;
        if (selectProjectid != null && !"".equals(selectProjectid)) {
            indicator = iIndicatorService.getById(selectProjectid);
        }
        map.put("selectProjectid", selectProjectid);
        map.put("indicator", indicator);
        return new JsonBean(200, "成功", map);
    }


    /**
     * 指标管理-选择数据源列表
     *
     * @param
     * @return
     */
    @GetMapping(value = "/zbjk/ds_data_list")
    @ApiOperation("指标管理-选择数据源列表 ")
    public JsonBean zbjk_datasource(@ApiParam(name = "pid", value = "pid") @RequestParam(value = "pid",required=false) String pid,

                                    @ApiParam(name = "pageNumber", value = "pageNumber") @RequestParam(value = "pageNumber") Integer pageNumber,

                                    @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token) {
        if(!ConstClass.checkToken(token)){
            return ConstClass.tokenFailure();
        }
        if (StringUtils.isBlank(pid)) {
            pid = "0";
        }
        Map map = new HashMap();
        IPage<TblBiDatasource> ip = new Page<>(pageNumber, ConstClass.DEFAULT_SIZE);
        QueryWrapper<TblBiDatasource> qw = new QueryWrapper<>();
        qw.eq("FATHERID", pid);
        qw.isNotNull("ISLEAF");
        qw.orderByDesc("CREATEDATE", "DSID");
        iTblBiDatasourceService.page(ip,qw);
        map.put("faflowid", pid);
        map.put("pageBean", ip);
        return new JsonBean(200, "成功", map);
    }


    /**
     * 指标管理-选择数据源左侧菜单
     *
     * @return
     */
    @GetMapping(value = "/zbjk/ds_data_left")
    @ApiOperation("指标管理-选择数据源左侧菜单")
    public JsonBean ds_data_left(@ApiParam(name = "orgid", value = "orgid") @RequestParam(value = "orgid",required = false) String orgid,
                                 @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token) {
        if(!ConstClass.checkToken(token)){
            return ConstClass.tokenFailure();
        }
        TblStaffUtil staff = null;
        TblOrganizationUtil attribute = null;
        TblOrganizationUtil attribute1 = null;
        try {
            staff = DealUserToken.parseUserToken(token);
            //选则的机构
            attribute = staff.getCurrentOrg();
            //当前用户的机构
            attribute1 = staff.getLinkOrg();
        } catch (Exception e) {
            e.printStackTrace();
        }
        String treeName = "";
        if (StringUtils.isEmpty(orgid)) {
            orgid = attribute.getOrgid().toString();
        }
        Organization o = treeService.belongToCompany(orgid);
        orgid = o.getOrgid().toString();
        treeName = o.getOrgname();
        final List<TblBiDatasource> list = iTblBiDatasourceService.getTreeList(orgid);
        String str = "";
        for (int i = 0; i < list.size(); i++) {
            TblBiDatasource bid = list.get(i);
            str += "tree.nodes['"+bid.getFatherid()+"_"+bid.getDsid()+"']=\"text:"+bid.getDsname()+";method:check("+bid.getDsid()+","+bid.getFatherid()+");\"\n";
            //str += "tree.nodes['"+ (i==0?-1:cat.getFatherriskcatid())+"_"+cat.getRiskcatid()+"']=\"text:"+cat.getRiskcatname()+";method:check("+cat.getRiskcatid()+","+(cat.getUnit()==null?-1:cat.getUnit())+","+(cat.getIsleaf()==null?0:cat.getIsleaf())+")\";\n";
        }
        Map map = new HashMap();
        map.put("tree", str);
        map.put("treeList",list);
        map.put("orgid", orgid);
        map.put("treeName", treeName);
        return new JsonBean(200, "成功", map);
    }


    /**
     * 指标管理-指标基准阈值列表
     *
     * @param
     * @return
     */
    @GetMapping(value = "/zbjk/kri_info_range_mng")
    @ApiOperation("指标管理-指标基准阈值列表")
    public JsonBean kri_info_range_mngZbjk(@ApiParam(name = "selectProjectid", value = "selectProjectid") @RequestParam(value = "selectProjectid") String selectProjectid,
                                           @ApiParam(name = "orgId", value = "orgId") @RequestParam(value = "orgId") String orgId,
                                           @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token) {
        if(!ConstClass.checkToken(token)){
            return ConstClass.tokenFailure();
        }
        List<Indicatorthreshold> hold = indicatorthresholdService.QueryByIndicatorId(selectProjectid);
        Map map = new HashMap();
        map.put("selectProjectid", selectProjectid);
        map.put("hold", hold);
        map.put("orgId", orgId);
        map.put("tableHistoryRows", hold.size());

        return new JsonBean(200, "成功", map);
    }


    /**
     * 指标管理-添加指标基本值域
     *
     * @param
     * @return
     */
    @PostMapping(value = "/zbjk/kri_info_range_save")
    @ApiOperation("指标管理-添加指标基本值域")
    public JsonBean add_zbzy(@ApiParam(name = "selectProjectid", value = "selectProjectid") @RequestParam(value = "selectProjectid") String selectProjectid,
                             @ApiParam(name = "sequencenumber", value = "sequencenumber") @RequestParam(value = "sequencenumber") String sequencenumber,
                             @ApiParam(name = "tolerance", value = "tolerance") @RequestParam(value = "tolerance") String tolerance,
                             @ApiParam(name = "thresholdname", value = "thresholdname") @RequestParam(value = "thresholdname") String thresholdname,
                             @ApiParam(name = "tolerancelower", value = "tolerancelower") @RequestParam(value = "tolerancelower") String tolerancelower,
                             @ApiParam(name = "toleranceupper", value = "toleranceupper") @RequestParam(value = "toleranceupper") String toleranceupper,
                             @ApiParam(name = "regionvalue", value = "regionvalue") @RequestParam(value = "regionvalue") String regionvalue,
                             @ApiParam(name = "prewarningmethod", value = "prewarningmethod") @RequestParam(value = "prewarningmethod") String prewarningmethod,
                             @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token) {
        if(!ConstClass.checkToken(token)){
            return ConstClass.tokenFailure();
        }
        Indicator in = new Indicator();
        if (selectProjectid != null) {
            in.setIndicatorid(new BigDecimal(selectProjectid));
        }
        Indicatorthreshold hold = new Indicatorthreshold();
        if (tolerancelower != null && tolerancelower.length() > 0) {
            hold.setTolerancelower(tolerancelower);
        }
        if (toleranceupper != null && toleranceupper.length() > 0) {
            hold.setToleranceupper(toleranceupper);
        }
        hold.setPrewarningmethod(prewarningmethod);
        hold.setRegionvalue(regionvalue);
        hold.setThresholdname(thresholdname);
        hold.setSequencenumber(sequencenumber);
        hold.setTolerance(tolerance);
        hold.setIndicatorid(in.getIndicatorid().intValue());
        indicatorthresholdService.save(hold);
        return new JsonBean(200, "成功", "");
    }

    /**
     * 指标管理-阈值删除
     *
     * @param
     * @return
     */
    @DeleteMapping(value = "zbjk/delete_zbglj_del")
    @ApiOperation("指标管理-阈值删除")
    public JsonBean delete_zbglj_del(@ApiParam(name = "thresholdid", value = "thresholdid") @RequestParam(value = "thresholdid") BigDecimal thresholdid,
                                     @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token) {
        if(!ConstClass.checkToken(token)){
            return ConstClass.tokenFailure();
        }
        if (thresholdid != null && !"".equals(thresholdid)) {
            Indicatorthreshold hold = indicatorthresholdService.getById(thresholdid);
            indicatorthresholdService.removeById(thresholdid);
        }
        return new JsonBean(200, "成功", "");
    }


    /**
     * 指标管理-修改权限判断
     *
     * @param
     * @return
     */
    @GetMapping(value = "/zbjk/indicatorcheckupdate")
    @ApiOperation("指标管理-修改权限判断")
    public JsonBean indicatorcheckUpdate(@ApiParam(name = "selectedid", value = "selectedid") @RequestParam("selectedid") String selectedid
            , @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token) {
        if(!ConstClass.checkToken(token)){
            return ConstClass.tokenFailure();
        }
        TblStaffUtil staff = null;
        TblOrganizationUtil attribute = null;
        TblOrganizationUtil attribute1 = null;
        try {
            staff = DealUserToken.parseUserToken(token);
            //选则的机构
            attribute = staff.getCurrentOrg();
            //当前用户的机构

        } catch (Exception e) {
            e.printStackTrace();
        }
        Indicator rule = this.iIndicatorService.getById(selectedid);
        if (rule.getStaffid().toString().equals(staff.getStaffid())) {
            return new JsonBean(200, "成功", "0");
        } else {
            return new JsonBean(200, "权限不足", "1");
        }
    }

    /**
     * 验证指标是否被预警使用
     *
     * @param
     * @return
     */
    @GetMapping(value = "/zbjk/kzjz_info_is")
    @ApiOperation("验证指标是否被预警使用")
    public JsonBean kzjz_info_is(@ApiParam(name = "incid", value = "incid") @RequestParam(value = "incid") String incid,
                                 @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token) {
        if(!ConstClass.checkToken(token)){
            return ConstClass.tokenFailure();
        }
        int num = 0;
        if (incid != null && !"".equals(incid)) {
            Indicator in = iIndicatorService.getById(incid);
            QueryWrapper<MonitorSolution> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("SOLUTIONID", in.getIndcatid());
            List<MonitorSolution> monitorSolutions = iMonitorSolutionService.list(queryWrapper);
            num = monitorSolutions.size();
            if (in != null && in.getRunstatus().toString().equals("2")) {
                num = 2;
            }
        }
        return new JsonBean(200, "成功", num + "");
    }


    /**
     * 指标管理-删除
     *
     * @param
     * @return
     */
    @DeleteMapping(value = "/zbjk/kri_info_del")
    @ApiOperation("指标管理-删除")
    @Transactional(rollbackFor = Exception.class)
    public JsonBean kri_info_modZbjk_del(@ApiParam(name = "selectProjectid", value = "指标主键") @RequestParam(value = "selectProjectid") String selectProjectid,
                                         @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token) {
        if(!ConstClass.checkToken(token)){
            return ConstClass.tokenFailure();
        }
        return iFlowService.deleteIndicatorById(selectProjectid);
    }


    /**
     * 指标管理-启用和禁用修改指标状态
     *
     * @param
     * @return
     */
    @GetMapping(value = "/zbjk/kri_info_status")
    @ApiOperation("指标管理-启用和禁用修改指标状态")
    public JsonBean kri_info_status(@ApiParam(name = "incid", value = "incid") @RequestParam(value = "incid") String incid,
                                    @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token) {
        if(!ConstClass.checkToken(token)){
            return ConstClass.tokenFailure();
        }
        if (incid != null && !"".equals(incid)) {
            Indicator indicator = iIndicatorService.getById(incid);
            if(Objects.isNull(indicator)){
                return new JsonBean(500, incid + "指标没有找到", 0);
            }
            if (indicator.getIndicatorstatus().equals("启用")) {
                indicator.setIndicatorstatus("禁用");
            } else {
                indicator.setIndicatorstatus("启用");
            }
            iIndicatorService.updateById(indicator);
        }
        return new JsonBean(200, "成功", 0);
    }


    /**
     * 指标管理 --结果
     *
     * @param pageNumber
     * @param
     * @return
     * @author SongXiangYing
     * @date 2016年1月31日 上午1:58:40
     */
    @GetMapping(value = "/zbjk/kri_info_result")
    @ApiOperation("指标管理 --结果")
    public JsonBean kri_info_reult(@ApiParam(name = "id", value = "id") @RequestParam(value = "id",required = false) BigDecimal id,
                                   @ApiParam(name = "execId", value = "execId") @RequestParam(value = "execId",required = false) String execId,
                                   @ApiParam(name = "pageNumber", value = "pageNumber") @RequestParam(value = "pageNumber", name = "pageNumber", defaultValue = "1") Integer pageNumber,

                                   @ApiParam(name = "source", value = "source") @RequestParam(value = "source",required = false) Integer source,
                                   @ApiParam(name = "mid", value = "mid") @RequestParam(value = "mid",required = false) String mid,
                                   @ApiParam(name = "orgId", value = "orgId") @RequestParam(value = "orgId",required = false) String orgId,
                                   @ApiParam(name = "fhtype", value = "fhtype") @RequestParam(value = "fhtype",required = false) String fhtype,
                                   @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token) {
        if(!ConstClass.checkToken(token)){
            return ConstClass.tokenFailure();
        }
        Indicator ind = iIndicatorService.getById(id.toString());

        if (StringUtils.isNotBlank(mid)) {
            mid = "1";
        }
        Map map = new HashMap();
        String sql=" select t.*,TBL_STAFF.USERNAME,TBL_STAFF.REALNAME,TI.INDICATORNAME from TBL_MONITOR_INDICATORRESULT  t left join  TBL_STAFF  on t.STAFFID = TBL_STAFF.STAFFID\n" +
                "left join TBL_INDICATOR TI on t.INDICATORID = TI.INDICATORID";
        // 指标
        IPage<Map<String,Object>>  page =new Page<>(pageNumber,ConstClass.DEFAULT_SIZE);
        String lastId = null;
        if (StringUtils.isNotBlank(mid)) {
            if (source.equals(MonitorSolutionresult.YJYJ)) {
                lastId = iWarningResultService.getLast();
            }

            sql+=" where indicatorid='"+id+"' and  source='"+source+"' " ;
            if (null != lastId) {
                sql+=" and executeId='"+lastId+"'" ;

            }
            sql+=" order by  saveTime desc ";

            map.put("pageBean",pageMapper.getPage(page,sql) );
        } else {
            sql+=" where SOLUTIONRESULTID=  "+id;
            sql+=" and executeId='"+execId+"'  ";

            map.put("pageBean",pageMapper.getPage(page,sql) );
            // 指标方案

        }

        map.put("id", id);
        map.put("mid", mid);
        map.put("orgId", orgId);
        map.put("ind", ind);
        map.put("execId", execId);
        map.put("source", source);
        map.put("fhtype", fhtype);
        String url = "/znjk/gzgl/indicatormodel";
        if (StringUtils.isNotBlank(mid)) {
            url = "/znjk/gzgl/indicator";
        }
        map.put("url", url);
        return new JsonBean(200, "成功", map);
    }


    /**
     * 行业指标库、指标管理,互相复制。
     *
     * @param
     * @return
     */
    @GetMapping(value = "/index/copyIndex")
    @ApiOperation("行业指标库、指标管理,互相复制")
    public JsonBean copyIndex(@ApiParam(name = "selectProjectid", value = "selectProjectid") @RequestParam(value = "selectProjectid") String selectProjectid,
                              @ApiParam(name = "orgid", value = "orgid") @RequestParam(value = "orgid") String orgid,
                              @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token
    ) {
        if(!ConstClass.checkToken(token)){
            return ConstClass.tokenFailure();
        }
        TblStaffUtil staff = null;
        TblOrganizationUtil attribute = null;
        TblOrganizationUtil attribute1 = null;
        try {
            staff = DealUserToken.parseUserToken(token);
            //选则的机构
            attribute = staff.getCurrentOrg();
            //当前用户的机构

        } catch (Exception e) {
            e.printStackTrace();
        }

        Indicator indicator = iIndicatorService.getById(selectProjectid);

        Indicator tblIndicator = new Indicator();
        if (indicator.getIndicatordb() == 0) {
            tblIndicator.setAuditingstatus(indicator.getAuditingstatus());
            tblIndicator.setConnectionstrings(indicator.getConnectionstrings());
            tblIndicator.setCreatedate(LocalDateTime.now());
            tblIndicator.setDepartmentincharge(indicator.getDepartmentincharge());
            tblIndicator.setForlumachs(indicator.getForlumachs());
            tblIndicator.setFormula(indicator.getFormula());
            tblIndicator.setFormulades(indicator.getFormulades());
            tblIndicator.setIndicatorcode(indicator.getIndicatorcode());
            tblIndicator.setIndicatordb(indicator.getIndicatordb());
            tblIndicator.setIndicatordes(indicator.getIndicatordes());
            tblIndicator.setIndicatorname(indicator.getIndicatorname());
            tblIndicator.setIndicatorstatus(indicator.getIndicatorstatus());
            tblIndicator.setMemo(indicator.getMemo());
            tblIndicator.setRunstatus(new BigDecimal(0));
            tblIndicator.setUnittype(indicator.getUnittype());
            //Organization tblOrganization = new Organization();
            //tblOrganization.setOrgid(new BigDecimal(orgid));
            //iOrganizationService.save(tblOrganization);
            tblIndicator.setOrgid(new BigDecimal(orgid).intValue());
            tblIndicator.setIndicatordb(Indicator.IS_HY1);
            tblIndicator.setStaffid(staff.getStaffid());
            iIndicatorService.save(tblIndicator);
            //复制阈值
            QueryWrapper<Indicatorthreshold> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("INDICATORID", selectProjectid);
            queryWrapper.orderByAsc("SEQUENCENUMBER");
            List<Indicatorthreshold> list = indicatorthresholdService.list(queryWrapper);
            for (Indicatorthreshold tblIndicatorthreshold : list) {
                Indicatorthreshold tbl = new Indicatorthreshold();
                tbl.setMemo(tblIndicatorthreshold.getMemo());
                tbl.setPrewarningmethod(tblIndicatorthreshold.getPrewarningmethod());
                tbl.setRegionvalue(tblIndicatorthreshold.getRegionvalue());
//                tbl.setScore(tblIndicatorthreshold.getScore());
                tbl.setSequencenumber(tblIndicatorthreshold.getSequencenumber());
                tbl.setThresholdname(tblIndicatorthreshold.getThresholdname());
                tbl.setTolerance(tblIndicatorthreshold.getTolerance());
                tbl.setToleranceupper(tblIndicatorthreshold.getToleranceupper());
                tbl.setTolerancelower(tblIndicatorthreshold.getTolerancelower());
                tbl.setIndicatorid(tblIndicator.getIndicatorid().intValue());
                indicatorthresholdService.save(tbl);
            }

            return new JsonBean(200, "成功", "复制成功！");

        } else {
            tblIndicator.setAuditingstatus(indicator.getAuditingstatus());
            tblIndicator.setConnectionstrings(indicator.getConnectionstrings());
            tblIndicator.setCreatedate(LocalDateTime.now());
            tblIndicator.setDepartmentincharge(indicator.getDepartmentincharge());
            tblIndicator.setForlumachs(indicator.getForlumachs());
            tblIndicator.setFormula(indicator.getFormula());
            tblIndicator.setFormulades(indicator.getFormulades());
            tblIndicator.setIndicatorcode(indicator.getIndicatorcode());
            tblIndicator.setIndicatordb(indicator.getIndicatordb());
            tblIndicator.setIndicatordes(indicator.getIndicatordes());
            tblIndicator.setIndicatorname(indicator.getIndicatorname());
            tblIndicator.setIndicatorstatus(indicator.getIndicatorstatus());
            tblIndicator.setMemo(indicator.getMemo());
            tblIndicator.setRunstatus(new BigDecimal(0));
            tblIndicator.setUnittype(indicator.getUnittype());

//            QueryWrapper<Organization> wrapper = new QueryWrapper<>();
//            wrapper.eq("ORGID", staff.getOrgid());
//            Organization organization = iOrganizationService.getById(wrapper);


            tblIndicator.setOrgid(staff.getLinkDetp().getOrgid().intValue());

            tblIndicator.setIndicatordb(Indicator.IS_HY0);
            tblIndicator.setStaffid(staff.getStaffid());
            iIndicatorService.save(tblIndicator);
            //复制阈值
            QueryWrapper<Indicatorthreshold> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("INDICATORID", selectProjectid);
            queryWrapper.orderByAsc("SEQUENCENUMBER");
            List<Indicatorthreshold> list = indicatorthresholdService.list(queryWrapper);
            for (Indicatorthreshold tblIndicatorthreshold : list) {

                Indicatorthreshold tbl = new Indicatorthreshold();
                tbl.setMemo(tblIndicatorthreshold.getMemo());
                tbl.setPrewarningmethod(tblIndicatorthreshold.getPrewarningmethod());
                tbl.setRegionvalue(tblIndicatorthreshold.getRegionvalue());
                //tbl.set
                //tbl.setScore(tblIndicatorthreshold.getScore());
                tbl.setSequencenumber(tblIndicatorthreshold.getSequencenumber());
                tbl.setThresholdname(tblIndicatorthreshold.getThresholdname());
                tbl.setTolerance(tblIndicatorthreshold.getTolerance());
                tbl.setToleranceupper(tblIndicatorthreshold.getToleranceupper());
                tbl.setTolerancelower(tblIndicatorthreshold.getTolerancelower());
                tbl.setIndicatorid(tblIndicator.getIndicatorid().intValue());
                indicatorthresholdService.save(tbl);

            }

            return new JsonBean(200, "成功", "复制成功！");

        }


    }

    /**
     * 指标管理-查看
     *
     * @param
     * @return
     */
    @SuppressWarnings("unchecked")
    @GetMapping(value = "/zbjk/kri_info_disp")
    @ApiOperation("指标管理-查看")
    public JsonBean kri_info_disp(@ApiParam(name = "selectProjectid", value = "selectProjectid") @RequestParam(value = "selectProjectid") String selectProjectid,@ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token) {
        if(!ConstClass.checkToken(token)){
            return ConstClass.tokenFailure();
        }
        Indicator indicator = null;
        List<Flow> fw = null;
        String flows = "";
        if (selectProjectid != null && !"".equals(selectProjectid)) {
            indicator = iIndicatorService.getById(selectProjectid);
            if(Objects.isNull(indicator)){
                return new JsonBean(500, selectProjectid + "没有找到指标",null);
            }

            fw = iFlowService.showListWithIndicatorid(indicator.getIndicatorid());
            for (Flow tblFlow : fw) {
                flows += tblFlow.getFlowid() + ",";
            }
        }
        Staff user = iStaffService.getById(indicator.getStaffid().toString());
        Map map = new HashMap();

        map.put("indicator", indicator);
        map.put("org", iOrganizationService.getById(indicator.getOrgid()));
        //全部flow
        map.put("flowList",fw);
        //所有编号
        map.put("flows", flows);
        map.put("creater", user);
        return new JsonBean(200, "成功", map);
    }


    /**
     * 指标管理-计算公式_查看
     *
     * @param
     * @return
     */
    @GetMapping(value = "/zbjk/kri_info_formula_disp")
    @ApiOperation("指标管理-计算公式_查看")
    public JsonBean kri_info_formula_disp(@ApiParam(name = "selectProjectid", value = "selectProjectid") @RequestParam(value = "selectProjectid") String selectProjectid,
                                          @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token) {
        if(!ConstClass.checkToken(token)){
            return ConstClass.tokenFailure();
        }
        Map map = new HashMap();
        Indicator indicator = null;
        if (selectProjectid != null && !"".equals(selectProjectid)) {
            indicator = iIndicatorService.getById(selectProjectid);
        }
        map.put("selectProjectid", selectProjectid);
        map.put("indicator", indicator);
        return new JsonBean(200, "成功", map);
    }


    /**
     * 指标管理-指标基准阈值列表-查看
     *
     * @param
     * @return
     */
    @GetMapping(value = "/zbjk/kri_info_range_mng_disp")
    @ApiOperation("指标管理-指标基准阈值列表-查看")
    public JsonBean kri_info_range_mng_disp(@ApiParam(name = "selectProjectid", value = "selectProjectid") @RequestParam(value = "selectProjectid") String selectProjectid,
                                            @ApiParam(name = "orgId", value = "orgId") @RequestParam(value = "orgId") String orgId,
                                            @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token) {
        if(!ConstClass.checkToken(token)){
            return ConstClass.tokenFailure();
        }
        List<Indicatorthreshold> hold = indicatorthresholdService.QueryByIndicatorId(selectProjectid);
        Map map = new HashMap();
        map.put("selectProjectid", selectProjectid);
        map.put("hold", hold);
        map.put("orgId", orgId);
        map.put("tableHistoryRows", hold.size());

        return new JsonBean(200, "成功", "0");
    }

    @Autowired
    IAccbookService iAccbookService;
    @Autowired
    CJobTaskService cJobTaskService;
    @Autowired
    JobTaskService jobTaskService;
    /**
     * 指标监控 - 指标管理  - 执行功能
     * @param request
     * @param response
     * @return
     */
    @GetMapping(value = "/gzjk/monitor/monitorExecute")
    @ApiOperation("指标管理-执行")
    public JsonBean monitorExecute(
            @ApiParam(name = "id", value = "指标主键") @RequestParam(value = "id") String id,
            @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token
    ) {

        if(!ConstClass.checkToken(token)){
            return ConstClass.tokenFailure();
        }

        try {
            TblStaffUtil staff = DealUserToken.parseUserToken(token);

            System.out.println(staff);
            BigDecimal staffid = staff.getStaffid();
            BigDecimal orgId = staff.getCurrentOrg().getOrgid();
            System.out.println(staffid);
            System.out.println(orgId);
            Accbook accBook = iAccbookService.findUserById(staffid, orgId);
            if(accBook == null){
                return new JsonBean(500, id + "没有在accbook表里找到相关项", "0");
            }

            String book = accBook.getAcctid();
            String bookid = accBook.getBookid();
            String bookyear = accBook.getBookyear();

            Indicator indicator = iIndicatorService.getById(id);
            if(Objects.isNull(indicator)){
                return new JsonBean(500, id + "没有找到对应的指标", "0");
            }

            if (indicator.getIndicatorstatus().equals("启用")) {
                if (indicator.getRunstatus().toString().equals("1")) {
                    return new JsonBean(200, "执行中，请稍等！",null);
                } else {
                    List<Indicator> list = new ArrayList<Indicator>();
                    indicator.setConnectionstrings(accBook == null ? indicator.getConnectionstrings() : bookid);
                    list.add(indicator);
                    ScheduleJob job = null;
                    try {
                        iIndicatorService.modifyStatusRun(list);
                        job = this.cJobTaskService.zbTaskExec(list, MonitorIndicatorresult.ZJ, null, iStaffService.getById(staffid));
                    } catch (SchedulerException e) {
                        e.printStackTrace();
                        try {
                            this.iIndicatorService.modifyStatusRunError(list);
                            this.jobTaskService.changeStatus(job, "stop");
                        } catch (SchedulerException e1) {
                            e1.printStackTrace();
                        }
                    }
                    return new JsonBean(200,"成功",null);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return  new JsonBean(500, "请先启用",null);
    }


}
